package net.pws.oos.web.dto;

import java.util.ArrayList;
import java.util.List;

import net.pws.common.beanutils.BeanUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.IndicatorTemplate;
import net.pws.oos.biz.model.IndicatorValue;
import net.pws.oos.biz.model.Organization;
import net.pws.oos.biz.model.Project;
import net.pws.oos.biz.model.ProspectRecod;
import net.pws.oos.biz.model.Task;
import net.pws.oos.biz.model.User;

import org.apache.commons.lang.StringUtils;

public class TaskDto extends Task {

	private static final long serialVersionUID = 1L;

	public static List<TaskDto> from(List<Task> taskList) {
		List<TaskDto> dto = new ArrayList<TaskDto>();
		for (Task task : taskList) {
			dto.add(from(task));
		}
		return dto;
	}

	public static TaskDto from(Task task) {
		if (task == null) {
			task = new Task();
		}
		String[] ignoredProps = { "district", "project", "executeOrganization",
				"template", 
				"prospectRecod", 
				"stationType", 
				"siteProperties", 
				"propertyOwnership", 
				"areaType", 
				"constructionModel", 
				"siteValid", 
				"creator", "executePerson", "result", "parent" };

		TaskDto dto = new TaskDto();
		BeanUtils.copyProperties(dto, task, ignoredProps);

		// District district = task.getDistrict();
		// if (district != null) {
		// dto.setDistrictId(district.getId());
		// dto.setDistrictName(district.getName());
		// }

		Project project = task.getProject();
		if (project != null) {
			dto.setProjectId(project.getId());
			dto.setProjectName(project.getName());
		}

		Organization executeOrganization = task.getExecuteOrganization();
		if (executeOrganization != null) {
			dto.setExecOrganId(executeOrganization.getId());
			dto.setExecOrganName(executeOrganization.getName());
		}

		User executePerson = task.getExecutePerson();
		if (executePerson != null) {
			dto.setExecPersonId(executePerson.getId());
			dto.setExecPersonName(executePerson.getName());
		}

		User creator = task.getCreator();
		if (creator != null) {
			dto.setCreatorId(creator.getId());
			dto.setCreatorName(creator.getName());
		}

		Task parent = task.getParent();
		if (parent != null) {
			dto.setParentId(parent.getId());
			dto.setParentName(parent.getName());
			dto.setParentCode(parent.getCode());
		}

		// TaskResult result = task.getResult();
		// if (result != null) {
		// dto.setResultId(result.getId());
		// }
		
		IndicatorTemplate template = task.getTemplate();
		if (template != null) {
			dto.setTemplateId(template.getId());
			dto.setTemplateName(template.getName());
		}
		ProspectRecod prospectRecod = task.getProspectRecod();
		if (prospectRecod != null) {
			dto.setProspectRecodId(prospectRecod.getId());
		}

		IndicatorValue stationType = task.getStationType();
		if (stationType != null) {
			dto.setStationTypeId(stationType.getId());
			dto.setStationTypeName(stationType.getName());
		}
		IndicatorValue siteProperties = task.getSiteProperties();
		if (siteProperties != null) {
			dto.setSitePropertiesId(siteProperties.getId());
			dto.setSitePropertiesName(siteProperties.getName());
		}
		IndicatorValue propertyOwnership = task.getPropertyOwnership();
		if (propertyOwnership != null) {
			dto.setPropertyOwnershipId(propertyOwnership.getId());
			dto.setPropertyOwnershipName(propertyOwnership.getName());
		}
		IndicatorValue areaType = task.getAreaType();
		if (areaType != null) {
			dto.setAreaTypeId(areaType.getId());
			dto.setAreaTypeName(areaType.getName());
		}
		IndicatorValue constructionModel = task.getConstructionModel();
		if (constructionModel != null) {
			dto.setConstructionModelId(constructionModel.getId());
			dto.setConstructionModelName(constructionModel.getName());
		}
		IndicatorValue siteValid = task.getSiteValid();
		if (siteValid != null) {
			dto.setSiteValidId(siteValid.getId());
			dto.setSiteValidName(siteValid.getName());
		}

		return dto;
	}

	// public static TaskDto from(Task task, List<Task> children) {
	// TaskDto dto = from(task);
	// List<TaskDto> subDtos = from(children);
	// dto.setChildren(subDtos);
	// return dto;
	// }

	public static Task to(Task task, TaskDto dto, EntityManager entityManager) {
		if (task == null) {
			task = new Task();
		}

		String[] ignoredProps = { "id", "status", "assignedDate", "district",
				"project", "executeOrganization", "executePerson", "creator",
				"createType",
				"description",
				"locateDate",
				"endDate",
				"longitude",
				"latitude",
				"locationProvice",
				"locationCity",
				"locationCounty",
				"locationStreet",
				"locationNumber",
				"locationAddress",
				"template",
				"prospectRecod",
				"stationType",
				"siteProperties",
				"propertyOwnership",
				"areaType",
				"constructionModel",
				"siteValid",
				"result", "parent" };
		BeanUtils.copyProperties(task, dto, ignoredProps);

		// if (StringUtils.isEmpty(dto.getCreatorId())) {
		// task.setCreator(null);
		// } else {
		// task.setCreator(entityManager.getReference(User.class,
		// dto.getCreatorId()));
		// }

		// if (StringUtils.isEmpty(dto.getDistrictId())) {
		// task.setDistrict(null);
		// }
		// else {
		// task.setDistrict(entityManager.find(District.class,
		// dto.getDistrictId()));
		// }

		if (StringUtils.isEmpty(dto.getProjectId())) {
			task.setProject(null);
		} else {
			task.setProject(entityManager.find(Project.class,
					dto.getProjectId()));
		}

		if (StringUtils.isEmpty(dto.getExecOrganId())) {
			task.setExecuteOrganization(null);
		} else {
			task.setExecuteOrganization(entityManager.find(Organization.class,
					dto.getExecOrganId()));
		}

		if (StringUtils.isEmpty(dto.getExecPersonId())) {
			task.setExecutePerson(null);
		} else {
			task.setExecutePerson(entityManager.find(User.class,
					dto.getExecPersonId()));
		}

		if (task.getExecuteOrganization() == null && task.getExecutePerson() != null) {
			task.setExecuteOrganization(task.getExecutePerson().getOrganization());
		}

		if (StringUtils.isEmpty(dto.getCreatorId())) {
			task.setCreator(null);
		} else {
			task.setCreator(entityManager.find(User.class, dto.getCreatorId()));
		}

		if (StringUtils.isEmpty(dto.getTemplateId())) {
			task.setTemplate(null);
		} else {
			task.setTemplate(entityManager.find(IndicatorTemplate.class,
					dto.getTemplateId()));
		}
		if (StringUtils.isEmpty(dto.getStationTypeId())) {
			task.setStationType(null);
		} else {
			task.setStationType(entityManager.find(IndicatorValue.class,
					dto.getStationTypeId()));
		}
		if (StringUtils.isEmpty(dto.getSitePropertiesId())) {
			task.setSiteProperties(null);
		} else {
			task.setSiteProperties(entityManager.find(IndicatorValue.class,
					dto.getSitePropertiesId()));
		}
		if (StringUtils.isEmpty(dto.getPropertyOwnershipId())) {
			task.setPropertyOwnership(null);
		} else {
			task.setPropertyOwnership(entityManager.find(IndicatorValue.class,
					dto.getPropertyOwnershipId()));
		}
		if (StringUtils.isEmpty(dto.getAreaTypeId())) {
			task.setAreaType(null);
		} else {
			task.setAreaType(entityManager.find(IndicatorValue.class,
					dto.getAreaTypeId()));
		}
		if (StringUtils.isEmpty(dto.getConstructionModelId())) {
			task.setConstructionModel(null);
		} else {
			task.setConstructionModel(entityManager.find(IndicatorValue.class,
					dto.getConstructionModelId()));
		}
		if (StringUtils.isEmpty(dto.getSiteValidId())) {
			task.setSiteValid(null);
		} else {
			task.setSiteValid(entityManager.find(IndicatorValue.class,
					dto.getSiteValidId()));
		}

		return task;
	}

	private String districtId;

	private String projectId;

	private String projectName;

	private String execOrganId;

	private String execOrganName;

	private String execPersonId;

	private String execPersonName;

	private String creatorId;

	private String creatorName;

	private String parentId;

	private String parentName;

	private String parentCode;

	private String resultId;

	private List<TaskDto> children;
	
    private List<AttachmentDto> attachments;

	private String templateId;
	private String templateName;
	private String prospectRecodId;
	private String stationTypeId;
	private String stationTypeName;
	private String sitePropertiesId;
	private String sitePropertiesName;
	private String propertyOwnershipId;
	private String propertyOwnershipName;
	private String areaTypeId;
	private String areaTypeName;
	private String constructionModelId;
	private String constructionModelName;
	private String siteValidId;
	private String siteValidName;

	public String getDistrictId() {
		return districtId;
	}

	public void setDistrictId(String districtId) {
		this.districtId = districtId;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getExecOrganId() {
		return execOrganId;
	}

	public void setExecOrganId(String execOrganId) {
		this.execOrganId = execOrganId;
	}

	public String getExecOrganName() {
		return execOrganName;
	}

	public void setExecOrganName(String execOrganName) {
		this.execOrganName = execOrganName;
	}

	public String getExecPersonId() {
		return execPersonId;
	}

	public void setExecPersonId(String execPersonId) {
		this.execPersonId = execPersonId;
	}

	public String getExecPersonName() {
		return execPersonName;
	}

	public void setExecPersonName(String execPersonName) {
		this.execPersonName = execPersonName;
	}

	public String getCreatorId() {
		return creatorId;
	}

	public void setCreatorId(String creatorId) {
		this.creatorId = creatorId;
	}

	public String getCreatorName() {
		return creatorName;
	}

	public void setCreatorName(String creatorName) {
		this.creatorName = creatorName;
	}

	public List<TaskDto> getChildren() {
		return children;
	}

	public void setChildren(List<TaskDto> children) {
		this.children = children;
	}

	public String getResultId() {
		return resultId;
	}

	public void setResultId(String resultId) {
		this.resultId = resultId;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getParentName() {
		return parentName;
	}

	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	public String getParentCode() {
		return parentCode;
	}

	public void setParentCode(String parentCode) {
		this.parentCode = parentCode;
	}

	public List<AttachmentDto> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<AttachmentDto> attachments) {
		this.attachments = attachments;
	}

	public String getTemplateId() {
		return templateId;
	}

	public void setTemplateId(String templateId) {
		this.templateId = templateId;
	}

	public String getTemplateName() {
		return templateName;
	}
	
	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}

	public String getProspectRecodId() {
		return prospectRecodId;
	}

	public void setProspectRecodId(String prospectRecodId) {
		this.prospectRecodId = prospectRecodId;
	}

	public String getStationTypeId() {
		return stationTypeId;
	}

	public void setStationTypeId(String stationTypeId) {
		this.stationTypeId = stationTypeId;
	}

	public String getStationTypeName() {
		return stationTypeName;
	}

	public void setStationTypeName(String stationTypeName) {
		this.stationTypeName = stationTypeName;
	}

	public String getSitePropertiesId() {
		return sitePropertiesId;
	}

	public void setSitePropertiesId(String sitePropertiesId) {
		this.sitePropertiesId = sitePropertiesId;
	}

	public String getSitePropertiesName() {
		return sitePropertiesName;
	}

	public void setSitePropertiesName(String sitePropertiesName) {
		this.sitePropertiesName = sitePropertiesName;
	}

	public String getPropertyOwnershipId() {
		return propertyOwnershipId;
	}

	public void setPropertyOwnershipId(String propertyOwnershipId) {
		this.propertyOwnershipId = propertyOwnershipId;
	}

	public String getPropertyOwnershipName() {
		return propertyOwnershipName;
	}

	public void setPropertyOwnershipName(String propertyOwnershipName) {
		this.propertyOwnershipName = propertyOwnershipName;
	}

	public String getAreaTypeId() {
		return areaTypeId;
	}

	public void setAreaTypeId(String areaTypeId) {
		this.areaTypeId = areaTypeId;
	}

	public String getAreaTypeName() {
		return areaTypeName;
	}

	public void setAreaTypeName(String areaTypeName) {
		this.areaTypeName = areaTypeName;
	}

	public String getConstructionModelId() {
		return constructionModelId;
	}

	public void setConstructionModelId(String constructionModelId) {
		this.constructionModelId = constructionModelId;
	}

	public String getConstructionModelName() {
		return constructionModelName;
	}

	public void setConstructionModelName(String constructionModelName) {
		this.constructionModelName = constructionModelName;
	}

	public String getSiteValidId() {
		return siteValidId;
	}

	public void setSiteValidId(String siteValidId) {
		this.siteValidId = siteValidId;
	}

	public String getSiteValidName() {
		return siteValidName;
	}

	public void setSiteValidName(String siteValidName) {
		this.siteValidName = siteValidName;
	}
}
